- if params[:period]
  = main_toolbar do |t|
    = t.export :trial_balance, item_label: :export_as_pdf.tl, key: Printers::TrialBalancePrinter.build_key(started_on: params[:started_on],
                                                                                                           stopped_on: params[:stopped_on],
                                                                                                           states: params[:states],
                                                                                                           natures: params[:natures],
                                                                                                           balance: params[:balance],
                                                                                                           accounts: params[:accounts],
                                                                                                           centralize: params[:centralize]) do |menu|
      - export_params = params.slice(:started_on, :stopped_on, :period, :states, :natures, :accounts, :centralize, :balance, :previous_year, :level_1, :level_2, :level_3, :level_4, :level_5, :level_6, :level_7, :level_8, :level_9, :level_10)
      - menu.item :export_as_ods.tl, params: export_params, format: :ods
      - menu.item :export_as_csv.tl, params: export_params, format: :csv
      - menu.item :export_as_xcsv.tl, params: export_params, format: :xcsv

= kujaku do |k|
  - k.helper :journal_period_crit, use_search_preference: true
  - k.helper :journal_entries_states_crit
  - k.helper :journals_natures_crit
  - k.helper do
    %label= :display_n_1.tl
    = check_box_tag("previous_year", "1", params[:previous_year] == "1")
  - k.helper do
    %label= :display_accounts.tl
    = radio_button_tag :balance, 'all', !params[:balance] || params[:balance] == 'all'
    %label= :all.tl
    = radio_button_tag :balance, 'balanced', params[:balance] == 'balanced'
    %label= :balanced.tl
    = radio_button_tag :balance, 'unbalanced', params[:balance] == 'unbalanced'
    %label= :unbalanced.tl
  - k.helper :accounts_range_crit
  - k.helper do
    - centralized_account = Nomen::Account.select { |a| a.centralizing }.collect {|a| a.send(Account.accounting_system)}.sort.join(" ")
    %label= :group_by_centralizing_accounts.tl
    = text_field_tag("centralize", params[:centralize]||centralized_account, size: 30)
  - k.helper do
    - max = Account.maximum("LENGTH(TRIM(number))").to_i
    %label= :subtotals_at_level.tl
    - for level in 1..max-1
      - name = "level_#{level}"
      = check_box_tag(name, "1", params[name]=="1")
      %label{ for: name }= level
- if @balance
  - currency = Preference[:currency]
  .active-list
    %table.list
      %thead
        %tr
          %th{rowspan: 2}= JournalEntryItem.human_attribute_name(:account_number)
          %th{rowspan: 2}= JournalEntryItem.human_attribute_name(:account_name)
          %th.center{colspan: 4, class: "totalcolor"}= :total.tl
          %th.center{colspan: 4, class: "balancecolor"}= :balance.tl
        %tr
          %th.right{class: "totalcolor"}= JournalEntry.human_attribute_name(:debit)
          %th.right{class: "totalcolor"}= JournalEntry.human_attribute_name(:credit)
          %th.right{class: "totalcolor"}= JournalEntry.human_attribute_name(:debit) + " N-1"
          %th.right{class: "totalcolor"}= JournalEntry.human_attribute_name(:credit) + " N-1"
          %th.right{class: "balancecolor"}= JournalEntry.human_attribute_name(:debit)
          %th.right{class: "balancecolor"}= JournalEntry.human_attribute_name(:credit)
          %th.right{class: "balancecolor"}= JournalEntry.human_attribute_name(:debit) + " N-1"
          %th.right{class: "balancecolor"}= JournalEntry.human_attribute_name(:credit) + " N-1"
      - @balance.each_with_index do |item, index|
        - d = item[0].size-1
        - prev_item = @prev_balance[index] || []
        - if item[1].to_i > 0
          - account = Account.find(item[1])
          %tr{class: cycle(:odd, :even)}
            %td.code= link_to(account.number, action: :show, controller: :accounts, id: account.id)
            %td= link_to(account.name, action: :show, controller: :accounts, id: account.id)
            %td.dec{class: "totalcolor"}= number_to_accountancy(item[2], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(item[3], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(prev_item[2], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(prev_item[3], currency)
            %td.dec{class: "balancecolor"}= number_to_accountancy(item[4], currency) if item[4].to_f > 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(-item[4].to_f, currency) if item[4].to_f < 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(prev_item[4], currency) if prev_item[4].to_f > 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(-prev_item[4].to_f, currency) if prev_item[4].to_f < 0
        - elsif item[1].to_i == -1
          %tr.total
            %th{colspan: 2}= :total.tl
            %td.dec{class: "totalcolor"}= number_to_accountancy(item[2], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(item[3], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(prev_item[7], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(prev_item[8], currency)
            %td.dec{class: "balancecolor"}= number_to_accountancy(item[5], currency)
            %td.dec{class: "balancecolor"}= number_to_accountancy(-item[6], currency) if item[6].to_f < 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(prev_item[5], currency)
            %td.dec{class: "balancecolor"}= number_to_accountancy(-prev_item[6], currency) if prev_item[6].to_f < 0
          %tr.subtotal
            %th{colspan: 2, style: "padding-left: #{d*8+4}px"}= :subtotal.tl(name: item[0])
            %td.dec{class: "totalcolor"}= number_to_accountancy(item[2], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(item[3], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(prev_item[2], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(prev_item[3], currency)
            %td.dec{class: "balancecolor"}= number_to_accountancy(item[4], currency) if item[4].to_f > 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(-item[4].to_f, currency) if item[4].to_f < 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(prev_item[4], currency) if prev_item[4].to_f > 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(-prev_item[4].to_f, currency) if prev_item[4].to_f < 0
        - elsif item[1].to_i == -3
          %tr.centralize{class: cycle(:odd, :even)}
            %td= item[0]
            %td= :centralized_account.tl(name: item[0])
            %td.dec{class: "totalcolor"}= number_to_accountancy(item[2], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(item[3], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(prev_item[2], currency)
            %td.dec{class: "totalcolor"}= number_to_accountancy(prev_item[3], currency)
            %td.dec{class: "balancecolor"}= number_to_accountancy(item[4], currency) if item[4].to_f > 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(-item[4].to_f, currency) if item[4].to_f < 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(prev_item[4], currency) if prev_item[4].to_f > 0
            %td.dec{class: "balancecolor"}= number_to_accountancy(-prev_item[4].to_f, currency) if prev_item[4].to_f < 0
.end
